package com.yanfan.zutai.controller;

import com.yanfan.common.annotation.Log;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.page.TableDataInfo;
import com.yanfan.common.enums.BusinessType;
import com.yanfan.common.utils.poi.ExcelUtil;
import com.yanfan.zutai.aop.SysInitData;
import com.yanfan.zutai.aop.SysInitUpdateData;
import com.yanfan.zutai.domain.BDeviceHistoryData;
import com.yanfan.zutai.service.IBDeviceHistoryDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备实时数据Controller
 *
 * @author ruoyi
 * @date 2022-05-28
 */
@RestController
@RequestMapping("/zutai/bDeviceHistoryData")
public class BDeviceHistoryDataController extends BaseController {
    @Autowired
    private IBDeviceHistoryDataService bDeviceHistoryDataService;
    @Value("${spring.datasource.druid.tdengine-server.enabled}")
    private Boolean enabled;

    /**
     * 查询设备实时数据列表,历史数据使用taos时序数据库查询
     */
    @GetMapping("/list")
    public TableDataInfo list(BDeviceHistoryData bDeviceHistoryData) {
        startPage();
        if (enabled) {
            return bDeviceHistoryDataService.selectBDeviceHistoryDataTaosList(bDeviceHistoryData);
        } else {
            return getDataTable(bDeviceHistoryDataService.selectBDeviceHistoryDataList(bDeviceHistoryData));
        }
    }

    /**
     * 导出设备实时数据列表
     */
    @Log(title = "设备实时数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BDeviceHistoryData bDeviceHistoryData) {
        TableDataInfo tableDataInfo = bDeviceHistoryDataService.selectBDeviceHistoryDataTaosList(bDeviceHistoryData);
        ExcelUtil<BDeviceHistoryData> util = new ExcelUtil<BDeviceHistoryData>(BDeviceHistoryData.class);
        List<BDeviceHistoryData> rows = (List<BDeviceHistoryData>) tableDataInfo.getRows();
        util.exportExcel(response, rows, "变量日志");
    }

    /**
     * 获取设备实时数据详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bDeviceHistoryDataService.selectBDeviceHistoryDataById(id));
    }

    /**
     * 新增设备实时数据
     */
    @Log(title = "设备实时数据", businessType = BusinessType.INSERT)
    @PostMapping
    @SysInitData
    public AjaxResult add(@RequestBody BDeviceHistoryData bDeviceHistoryData) {
        return toAjax(bDeviceHistoryDataService.insertBDeviceHistoryData(bDeviceHistoryData));
    }

    /**
     * 修改设备实时数据
     */
    @Log(title = "设备实时数据", businessType = BusinessType.UPDATE)
    @PutMapping
    @SysInitUpdateData
    public AjaxResult edit(@RequestBody BDeviceHistoryData bDeviceHistoryData) {
        return toAjax(bDeviceHistoryDataService.updateBDeviceHistoryData(bDeviceHistoryData));
    }

    /**
     * 删除设备实时数据
     */
    @Log(title = "设备实时数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bDeviceHistoryDataService.deleteBDeviceHistoryDataByIds(ids));
    }
}
